package com.veo.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA() throws InterruptedException {
        //线程数测试 比如a请求过来,处理很慢,在一直处理,此时b请求又过来了
        //		此时因为a占用一个线程,此时要处理b请求就只有额外开启一个线程
        //		那么就会报错
        //TimeUnit.MICROSECONDS.sleep(800);
        return "------------testA";
    }

    @GetMapping("/testB")
    public String testB(){
        return "------------testB";
    }

    @GetMapping("/testC")
    public String testC() throws InterruptedException {
        //服务降级测试，暂停一秒钟
        TimeUnit.SECONDS.sleep(2);
        log.info("测试C 测试RT");
        return "------------testC";
    }

    @GetMapping("/testD")
    public String testD() throws InterruptedException {
        //异常测试
        int i = 10 / 0;
        log.info("测试D 测试异常");
        return "------------testD";
    }

    @GetMapping("/testHotKey")
    //定义一个资源名字和服务熔断处理的方法，达到sentinel控制台设定的方法就熔断
    @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
                             @RequestParam(value = "p2",required = false)String p2){
        return "---------testHotKey";
    }

    public String deal_testHotKey(String p1, String p2, BlockException blockException){
        return "deal_testHotKeyo(╥﹏╥)o"+p1+p2+blockException.getMessage();
    }
}
